package com.spark.cust.lesson

/**
 * @description:
 * @time: 2020/9/27 21:29
 * @author: lhy
 */
object homework {
    def main(args: Array[String]): Unit = {

    }
    // 求从1加到100的和
    def sumInt(x:Int): Unit = {
        var sum = 0
        for (i <- 1 to 100) sum += i
        println(sum)
    }
    // 求从1加到100的奇数和
    def sumOddNumber(x:Int):Unit = {
        var sum = 0
        for (i <- 1 to 100 if i%2 != 0) sum += i
        println(sum)
    }
    // 求从1到10的阶乘和
    def factorial(x : Int): Int =
        if (x == 0) 1 else x * factorial(x - 1)
    def factorialSum(n:Int):Unit = {
        var sum:Int = 0
        for (i <- 1 to n) sum += factorial(i)
        println(sum)
    }
    // 用循环语句输出99乘法表
    def printMultiplicationTable():Unit = {
        for (i <- 1 to 9;j <- 1 to i) {
            print(s"$j" + "*" +s"$i" + "=" + i*j +"  " )
            if (i == j)
                println()
        }
    }
    // 用数组输出100以内的素数（使用yield）
    def isPrimeNumber(i:Int): Boolean = {
        for (j <- 2 to  Math.sqrt(i).toInt){
            if (i%j == 0)  return false
        }
        true
    }
    def getPrimeNumber(x:Int): Array[Int] = {
        for (i <- Array.range(2,x + 1) if (isPrimeNumber(i))) yield i
    }
    // 用数组输出1000以内能被7和9整除的数（使用yield）
    def divisible(x:Int,firstDivisor:Int,secondDivisor:Int):Array[Int] = {
        for (i <- Array.range(1,x + 1) if i%firstDivisor == 0 && i%secondDivisor == 0) yield i
    }
    //求表达式“1+1/2 + 1/3 + 1/4 +... +1/n” 的结果（n为任意给定数）
    def reciprocalSum(n:Int): Double = {
        var sum:Double = 0
        for (i <- 1 to n) sum += 1.0/i
        sum
    }
}
